class Toast {
  constructor (config = { mask: true }) {
    this.config = { ...config }
  }

  info (title) {
    return uni.showToast({ title, icon: 'none', ...this.config })
  }

  success (title) {
    uni.showToast({ title, icon: 'success', ...this.config })
  }

  error (title) {
    return uni.showToast({ title, icon: 'error', ...this.config })
  }
}
export const toast = new Toast()

export const loading = (title = '') => {
  return uni.showLoading({ title, mask: true })
}

function isPromise (obj) {
  return (
    !!obj &&
    (typeof obj === 'object' || typeof obj === 'function') &&
    typeof obj.then === 'function'
  )
}
export const useArrayReturn = () => {
  uni.addInterceptor({
    returnValue (res) {
      if (!isPromise(res)) {
        return res
      }
      const returnValue = [undefined, undefined]
      return res
        .then((res) => {
          returnValue[1] = res
        })
        .catch((err) => {
          returnValue[0] = err
        })
        .then(() => returnValue)
    }
  })
}
